<?php

class m_groupdiv extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					groupdiv_nama,
					groupdiv_id
				FROM m_groupdiv
				WHERE 
					groupdiv_active = 'y'
		";
		
		if (@$groupdiv_nama != "") $sql .= " AND groupdiv_nama like '%".@$groupdiv_nama."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('m_groupdiv',$data);
		return 1;
	}
	
	function _update($data,$id,$divisi){
		$this->db->where('groupdiv_id',$id);
		$this->db->update('m_groupdiv',$data);
        
		/* null kan flowdet s_users = user_id */
		$this->db->where('s_users_id',$id);
		$this->db->update('m_flowdet',array("s_users_id" => null));
		
		/* update yang di centang */
		for($i=0; $i < count($divisi); $i++){
			$this->db->where('flowdet_id',$divisi[$i]);
			$this->db->update('m_flowdet',array("s_users_id" => $id));
		}
        
		return 1;
	}
	
	function _delete($data,$id){
		$this->db->where('groupdiv_id',$id);
		$this->db->update('m_groupdiv',$data);
		return 1;
	}
}